/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.FeeBillBean;
import com.minierp.bean.FeeBillItemBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.FeeBillDao;
import com.minierp.dao.FeeBillItemDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.DataChangedException;
import com.minierp.err.NotAllowException;
import com.minierp.helper.PageHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import com.minierp.model.BusinessProfit;
import com.minierp.model.FeeBill;
import com.minierp.model.FeeBillItem;
import com.minierp.model.SystemLog;
import com.minierp.service.FeeBillService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class FeeBillServiceImpl implements FeeBillService {
	public void txInsert(FeeBillBean form, FeeBillDao feeBillDao,
			FeeBillItemDao feeBillItemDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BusinessBillDao businessBillDao, SerialNumberDao serialNumberDao,
			IdGenerator idGenerator) throws Exception {
		FeeBill obj = form.getFeeBill();
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(FeeBill.class.getName()));
		if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao,
					form.getCodeRule(), "", form.getFeeBill().getTallySetId()));

			for (FeeBill d = feeBillDao.get(obj.getBillCode()); d != null; d = feeBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
						.getCodeRule(), "", form.getFeeBill().getTallySetId()));
			}
		}

		if (obj.getExamined().equals("Y")) {
			obj.setExamSms("N");
			obj.setExamineTime(new Date());
		} else {
			obj.setExamSms("Y");
		}

		feeBillDao.save(obj);
		this.saveItems(form.getFeeBill(), form.getItems(), feeBillItemDao,
				idGenerator);
		if (obj.getExamined().equals("Y")) {
			PageHelper.updateBankAccount(obj.getBankAccountId(),
					obj.getTallySetId(), obj.getId(), obj.getCost(),
					obj.getFeeDate(), obj.getExamineTime(), obj.getBillCode(),
					"FEE_BILL", "", "费用", "P", obj.getManagerName(),
					obj.getMark(), "FEE", "SAVE_EXAM", false,
					(BankAccountDetailDao) null, bankAccountBalanceDao,
					idGenerator);
		}

		if (obj.getExamined().equals("Y")) {
			this.updateBbProf("ADD", feeBillItemDao.getList(obj.getId()),
					businessBillDao, idGenerator);
		}

	}

	private void saveItems(FeeBill bill, List items,
			FeeBillItemDao feeBillItemDao, IdGenerator idGenerator)
			throws NotAllowException {
		int newNum = 0;

		for (int curItemId = 0; curItemId < items.size(); ++curItemId) {
			FeeBillItemBean bean = (FeeBillItemBean) items.get(curItemId);
			if (bean.getStatus().equals("NEW")) {
				++newNum;
			}
		}

		long arg9 = 0L;
		if (newNum > 0) {
			arg9 = idGenerator.getId(FeeBillItem.class.getName(), newNum);
		}

		for (int i = 0; i < items.size(); ++i) {
			FeeBillItemBean bean1 = (FeeBillItemBean) items.get(i);
			bean1.getFeeBillItem().setBillId(bill.getId());
			if (bean1.getStatus().equals("DELETE")) {
				if (bean1.getFeeBillItem().getId() != 0L) {
					feeBillItemDao.delete(bean1.getFeeBillItem().getId());
				}
			} else if (bean1.getStatus().equals("EDIT")) {
				feeBillItemDao.update(bean1.getFeeBillItem());
			} else if (bean1.getStatus().equals("NEW")) {
				bean1.getFeeBillItem().setId(arg9);
				feeBillItemDao.save(bean1.getFeeBillItem());
				++arg9;
			}
		}

	}

	private void updateBbProf(String countType, List items,
			BusinessBillDao businessBillDao, IdGenerator idGenerator) {
		for (int i = 0; i < items.size(); ++i) {
			FeeBillItem item = (FeeBillItem) items.get(i);
			if (item.getBbId() != 0L) {
				BusinessBill bb = (BusinessBill) businessBillDao.get(
						BusinessBill.class, item.getBbId());
				if (bb != null) {
					BusinessBillExt be = (BusinessBillExt) businessBillDao.get(
							BusinessBillExt.class, bb.getId());
					BusinessProfit p = businessBillDao.getBusinessProfit(bb
							.getBillCode());
					if (p == null) {
						p = new BusinessProfit();
						p.setBbCode(bb.getBillCode());
						p.setId(idGenerator.getId(BusinessProfit.class
								.getName()));
						p.setCooperCost(new BigDecimal(0));
						p.setMaterialCost(new BigDecimal(0));
						if (countType.equals("SUB")) {
							p.setFeeCost((new BigDecimal(0)).subtract(item
									.getCost()));
						} else {
							p.setFeeCost(item.getCost());
						}

						p.setProfitCost(bb.getCost().add(be.getAdjustCost())
								.subtract(p.getFeeCost()));
						businessBillDao.save(p);
					} else {
						if (countType.equals("SUB")) {
							p.setFeeCost(p.getFeeCost()
									.subtract(item.getCost()));
							p.setProfitCost(p.getProfitCost().add(
									item.getCost()));
						} else {
							p.setFeeCost(p.getFeeCost().add(item.getCost()));
							p.setProfitCost(p.getProfitCost().subtract(
									item.getCost()));
						}

						businessBillDao.update(p);
					}
				}
			}
		}

	}

	public void txUpdate(FeeBillBean form, FeeBillDao feeBillDao,
			FeeBillItemDao feeBillItemDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BusinessBillDao businessBillDao, IdGenerator idGenerator)
			throws Exception {
		FeeBill obj = form.getFeeBill();
		Object[] sv = feeBillDao.getStatusValues(obj.getId());
		if (sv == null) {
			throw new Exception("Can\'t get status values for fee bill:"
					+ obj.getId());
		} else {
			long existSycVal = ((Date) sv[0]).getTime();
			String exitEnabled = sv[1].toString();
			String existExamined = sv[2].toString();
			if (existSycVal != form.getSychronizeValue()) {
				throw new DataChangedException("费用单已被其他操作修改，请关闭页面再打开后重新操作！");
			} else if (exitEnabled.equals("N")) {
				throw new NotAllowException("费用单已作废，不能修改！");
			} else if (existExamined.equals("Y")) {
				throw new NotAllowException("费用单已审核，不能修改！");
			} else {
				if (obj.getExamined().equals("Y")) {
					obj.setExamSms("N");
					obj.setExamineTime(new Date());
				} else {
					obj.setExamSms("Y");
				}

				obj.setUpdateTime(new Date());
				feeBillDao.update(obj);
				this.saveItems(form.getFeeBill(), form.getItems(),
						feeBillItemDao, idGenerator);
				if (obj.getExamined().equals("Y")) {
					PageHelper.updateBankAccount(obj.getBankAccountId(),
							obj.getTallySetId(), obj.getId(), obj.getCost(),
							obj.getFeeDate(), obj.getExamineTime(),
							obj.getBillCode(), "FEE_BILL", "", "费用", "P",
							obj.getManagerName(), obj.getMark(), "FEE",
							"SAVE_EXAM", false, (BankAccountDetailDao) null,
							bankAccountBalanceDao, idGenerator);
				}

				if (obj.getExamined().equals("Y")) {
					this.updateBbProf("ADD",
							feeBillItemDao.getList(obj.getId()),
							businessBillDao, idGenerator);
				}

			}
		}
	}

	public void txExamine(long id, UserBean ub, FeeBillDao feeBillDao,
			FeeBillItemDao feeBillItemDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BusinessBillDao businessBillDao, IdGenerator idGenerator)
			throws Exception {
		FeeBill a = (FeeBill) feeBillDao.get(FeeBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("N")) {
			a.setExamined("Y");
			a.setExamSms("N");
			a.setExamineTime(new Date());
			a.setExaminerId(ub.getStaffer().getId());
			a.setExaminerName(ub.getStaffer().getStafferName());
			a.setUpdateTime(new Date());
			feeBillDao.update(a);
			PageHelper.updateBankAccount(a.getBankAccountId(),
					a.getTallySetId(), a.getId(), a.getCost(), a.getFeeDate(),
					a.getExamineTime(), a.getBillCode(), "FEE_BILL", "", "费用",
					"P", a.getManagerName(), a.getMark(), "FEE", "EXAM", false,
					(BankAccountDetailDao) null, bankAccountBalanceDao,
					idGenerator);
			this.updateBbProf("ADD", feeBillItemDao.getList(a.getId()),
					businessBillDao, idGenerator);
		}

	}

	public void txUnExamine(long id, UserBean ub, FeeBillDao feeBillDao,
			FeeBillItemDao feeBillItemDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			BusinessBillDao businessBillDao, IdGenerator idGenerator)
			throws Exception {
		FeeBill a = (FeeBill) feeBillDao.get(FeeBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("Y")) {
			a.setExamined("N");
			a.setExamSms("Y");
			a.setExaminerId(0L);
			a.setExaminerName("");
			a.setUpdateTime(new Date());
			feeBillDao.update(a);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String mark = ub.getStaffer().getStafferName() + "于"
					+ sf.format(new Date()) + "反审核单据";
			PageHelper.updateBankAccount(a.getBankAccountId(),
					a.getTallySetId(), a.getId(),
					(new BigDecimal(0)).subtract(a.getCost()), a.getFeeDate(),
					new Date(), a.getBillCode(), "FEE_BILL", "", "费用", "P",
					a.getManagerName(), mark, "FEE", "UN_EXAM", true,
					bankAccountDetailDao, bankAccountBalanceDao, idGenerator);
			this.updateBbProf("SUB", feeBillItemDao.getList(a.getId()),
					businessBillDao, idGenerator);
		}

	}

	public void txCancel(long id, String reason, UserBean ub,
			FeeBillDao feeBillDao, FeeBillItemDao feeBillItemDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			BusinessBillDao businessBillDao, IdGenerator idGenerator)
			throws Exception {
		FeeBill d = (FeeBill) feeBillDao.get(FeeBill.class, id);
		if (d != null && d.getEnabled().equals("Y")) {
			d.setEnabled("N");
			d.setExamSms("N");
			d.setDisableManId(ub.getUser().getStafferId());
			d.setUpdateTime(new Date());
			feeBillDao.update(d);
			if (d.getExamined().equals("Y")) {
				SimpleDateFormat log = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String mark = ub.getStaffer().getStafferName() + "于"
						+ log.format(new Date()) + "作废单据";
				PageHelper.updateBankAccount(d.getBankAccountId(),
						d.getTallySetId(), d.getId(),
						(new BigDecimal(0)).subtract(d.getCost()),
						d.getFeeDate(), new Date(), d.getBillCode(),
						"FEE_BILL", "", "费用", "P", d.getManagerName(), mark,
						"FEE", "CANC", true, bankAccountDetailDao,
						bankAccountBalanceDao, idGenerator);
			}

			if (d.getExamined().equals("Y")) {
				this.updateBbProf("SUB", feeBillItemDao.getList(d.getId()),
						businessBillDao, idGenerator);
			}

			SystemLog log1 = new SystemLog();
			log1.setId(idGenerator.getId(SystemLog.class.getName()));
			log1.setActionCode("FEE_BILL");
			log1.setLogType("CANCEL_LOG");
			log1.setCreaterId(ub.getStaffer().getId());
			log1.setCreaterName(ub.getStaffer().getStafferName());
			log1.setDepName(ub.getDepName());
			log1.setDepId(ub.getStaffer().getDepartmentId());
			log1.setStatus("S");
			log1.setUserName(ub.getUser().getUserName());
			log1.setIp(d.getBillCode());
			log1.setMark("");
			log1.setRelId(id);
			log1.setCancelReason(reason);
			log1.setTallySetId(d.getTallySetId());
			feeBillDao.save(log1);
		}

	}
}